package List;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-04-06
 * Time:19:16
 */
public class MyList {

   public int size=0;
    private class List{
        //	存储数据
        int item;
        //	下一个结点
        List next;
        public List(int item) {
            this.item = item;
        }
        public List() {};
    }
    private List head;
     /* public void creatList()
      {
          List kk=new List(12);
          List kk1=new List(13);
          List kk2=new List(14);
          List kk3=new List(15);
        kk.next=kk1;
        kk1.next=kk2;
        kk2.next=kk3;
        kk3=null;
        this.head=kk;
      }*/
    public int size(){
        List cur=head;
        int size=0;
        while(cur!=null)
        {
           size++;
            cur=cur.next;
        }
        return size;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        List cur=head;
        while(cur!=null)
        {
            if(cur.item==key)
                cur=cur.next;
                return true;

        }
          return false;
    }

    //头插法
    void inserthead(int sum)
    {
        List new1=new List(sum);
        new1.next=head;
        head=new1;
        size++;
    }
    //尾插法
    public void insertTail(int sum) {

        List cur = head;
        List new1 = new List(sum);
        if(cur==null)
        {
            head=new1;
        }
       else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = new1;
            new1.next = null;
        }
       size++;
    }

    //展示
    public void Show()
    {
        List cur=head;
        while(cur!=null)
        {
            System.out.print(cur.item+" ");
            cur =cur.next;
        }
        System.out.println("");
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index<0||index>size-1)
        {
            System.out.println("非法");
            return;
        }
        List cur=head;
        int i=0;
        while(i<index-1)
        {
            cur=cur.next;
            i++;
        }
        List fw=new List(data);
        fw.next=cur.next;
        cur.next=fw;
    }
    //头删法
    public void deletehead()
    {
        if(head==null)
        {
            return;
        }
       head=head.next;
    }
    //尾删法
    public void deleteTail()
    {
        List cur = head;
        if(cur==null)
        {
            System.out.println("删除错误");
            return;
        }
        else {
            while (cur.next.next != null) {
                cur = cur.next;
            }
           cur.next=null;
        }

    }
    //删除元素
    public void deleteSum(int data)
    {
        List cur=head;
        while(cur!=null)
        {
            if(cur.next.item==data) {
                List kk=cur.next.next;
                cur.next=kk;
                break;
            }
            cur = cur.next;
        }
       return;
    }
    public void deleteNum(int data)
    {
        if(head==null)
        {
            return;
        }
        List front=head.next;
        List behind=head;
        while(front!=null)
        {
            if(front.item==data) {
              behind.next= front.next;
              front = front.next;
            }
            else{
                behind=front;
                front = front.next;
            }
        }
        if(head.item==data)
        {
            head=head.next;
        }
    }
}
